package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.UserManager;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.adapter.ADAddGoodInfoAdapter;
import com.zlzxm.shoesmarket.view.adapter.ADSearchHistory;
import com.zlzxm.shoesmarket.view.adapter.entity.EntitySearchHistory;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMAddGoods;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * CREATE BY zlzxm ON 4/15/21
 * <br>添加商品
 */
public class ACAddGoods extends BaseActivity implements OnLoadMoreListener, OnItemClickListener {

    public static void start(Context context) {
        Intent starter = new Intent(context, ACAddGoods.class);
        context.startActivity(starter);
    }

    private VMAddGoods vmAddGoods;

    private RecyclerView rvGoods,rvSearchHistory;

    private TextView tvHistoryEmpty;

    private ADAddGoodInfoAdapter adAddGoodInfoAdapter = new ADAddGoodInfoAdapter();

    private ADSearchHistory adSearchHistory = new ADSearchHistory();


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_add_goods);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("添加商品");
        simpleHead.setOnItemclicklistener(this::finish);

        rvGoods =  ZViewHelp.findById(this,R.id.rvGoods);

        rvGoods.setLayoutManager(new LinearLayoutManager(this));

        rvSearchHistory =  ZViewHelp.findById(this,R.id.rvSearchHistory);

        rvSearchHistory.setLayoutManager(new FlexboxLayoutManager(this));

        tvHistoryEmpty = ZViewHelp.findById(this,R.id.tvHistoryEmpty);

        rvGoods.setAdapter(adAddGoodInfoAdapter);

        adAddGoodInfoAdapter.getLoadMoreModule().setOnLoadMoreListener(this);
        adAddGoodInfoAdapter.getLoadMoreModule().setEnableLoadMore(true);
        adAddGoodInfoAdapter.getLoadMoreModule().setAutoLoadMore(true);
        adAddGoodInfoAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
        adAddGoodInfoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmAddGoods = new ViewModelProvider(this).get(VMAddGoods.class);
        vmAddGoods.getLiveGoods().observe(this,adAddGoodInfoAdapter::setList);
        vmAddGoods.getLiveHistory().observe(this,this::onChanged);
        vmAddGoods.getLiveErrorToast().observe(this,this::errorToast);
        vmAddGoods.liveIsLoadEnd.observe(this, aBoolean -> adAddGoodInfoAdapter.getLoadMoreModule().loadMoreEnd());

        vmAddGoods.liveIsLoadEnd.observe(this, aBoolean -> {
            if(aBoolean) {
                adAddGoodInfoAdapter.getLoadMoreModule().loadMoreComplete();
            }else {
                adAddGoodInfoAdapter.getLoadMoreModule().loadMoreFail();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        rvSearchHistory.setAdapter(adSearchHistory);
        vmAddGoods.requestGoods();
        vmAddGoods.requestHistory();
    }

    private void onChanged(List<EntitySearchHistory> it) {

        if (it == null || it.size() < 1) {

            tvHistoryEmpty.setVisibility(View.VISIBLE);
            rvSearchHistory.setVisibility(View.GONE);

        } else {
            tvHistoryEmpty.setVisibility(View.GONE);
            rvSearchHistory.setVisibility(View.VISIBLE);

        }

        adSearchHistory.setList(it);

    }

    @Override
    public void onLoadMore() {

        vmAddGoods.requestGoods();
    }

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        String code = adAddGoodInfoAdapter.getData().get(position).getCode();
        String url = adAddGoodInfoAdapter.getData().get(position).getFace();
        String name = adAddGoodInfoAdapter.getData().get(position).getTitle();
        String brand = adAddGoodInfoAdapter.getData().get(position).getBrand();
        String tags = adAddGoodInfoAdapter.getData().get(position).getTags();
        ActivityIndex.startACRepositoryGoodsDetail(this,view.findViewById(R.id.ivFace),"",url,name,code,brand,tags);
    }
}
